package com.example.client;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;


import java.util.ArrayList;
import java.util.List;

import entity.Contacter;

public class ContactAddActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_add;
    private Button btn_query;
    private EditText et_name;
    private EditText et_number;
    private EditText et_mail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_contact_add);
        et_name = findViewById(R.id.et_name);
        et_number = findViewById(R.id.et_number);
        et_mail = findViewById(R.id.et_mail);
        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_query).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_add){
            Contacter contacter = new Contacter();
            contacter.setName(et_name.getText().toString());
            contacter.setMail(et_mail.getText().toString());
            contacter.setNmber(et_number.getText().toString());
            // 使用ContactResolver 多次写入，每次写一个字段
            //addContacts(getContentResolver(),contacter);
            addFullContacts(getContentResolver(),contacter); // 批处理

        }else if (v.getId() == R.id.btn_query){

        }
    }
// 一次性全部插入联系人
    private void addFullContacts(ContentResolver contentResolver, Contacter contacter) {
        ContentProviderOperation op_main = ContentProviderOperation
                .newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME,null)
                .build();

        ContentProviderOperation op_name =
                ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Contacts.Data.RAW_CONTACT_ID,0)
                        .withValue(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.Data.DATA2,contacter.getName())
                        .build();
        ContentProviderOperation op_phone =
                ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Contacts.Data.RAW_CONTACT_ID,0)
                        .withValue(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.Data.DATA1,contacter.getNmber())
                        .withValue(ContactsContract.Contacts.Data.DATA2, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                        .build();
        ContentProviderOperation op_mail =
                ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Contacts.Data.RAW_CONTACT_ID,0)
                        .withValue(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.Data.DATA1,contacter.getMail())
                        .withValue(ContactsContract.Contacts.Data.DATA2, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
                        .build();

        ArrayList<ContentProviderOperation> operations = new ArrayList<>();
        operations.add(op_main);
        operations.add(op_name);
        operations.add(op_phone);
        operations.add(op_mail);

        try {
            contentResolver.applyBatch(ContactsContract.AUTHORITY,operations);
        } catch (OperationApplicationException e) {
            throw new RuntimeException(e);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    // 往手机通讯录里面添加联系人
    private void addContacts(ContentResolver resolver, Contacter contacter) {
        ContentValues values = new ContentValues();

        Uri uri = resolver.insert(ContactsContract.RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(uri);
        ContentValues name = new ContentValues();
        // 关联联系人编号
        name.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID,rawContactId);
        // 姓名的数据类型
        name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
        //联系人的姓名
        name.put(ContactsContract.Contacts.Data.DATA2,contacter.getName());
        resolver.insert(ContactsContract.Data.CONTENT_URI,name);

        ContentValues phone = new ContentValues();
        // 关联联系人编号
        phone.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID,rawContactId);
        // 姓名的数据类型
        phone.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
        //联系人的姓名
        phone.put(ContactsContract.Contacts.Data.DATA2,contacter.getNmber());
        resolver.insert(ContactsContract.Data.CONTENT_URI,phone);

        ContentValues mail = new ContentValues();
        // 关联联系人编号
        mail.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID,rawContactId);
        // 邮箱的数据类型
        mail.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);
        //联系人的姓名
        mail.put(ContactsContract.Contacts.Data.DATA1,contacter.getMail());
        resolver.insert(ContactsContract.Data.CONTENT_URI,mail);

    }
}